(in-package #:lilu-terminal)

;;;
;;; simple terminal program
;;;

(defun %make-terminal-args (cmd-single-p cmd)
  (if cmd
      (uiop:with-temporary-file (:stream strm :pathname file :keep t)
        (format strm (uiop:escape-sh-command cmd))
        (format strm "~%rm ~a" (uiop:unix-namestring file))
        :close-stream
        (if cmd-single-p
            (list "-e"
                  (format nil "sh ~a" (uiop:unix-namestring file)))
            (list "-e" "sh"
                  (format nil "~a" (uiop:unix-namestring file)))))
      nil))

(defclass simple-terminal-program-application (lilu-app:simple-program-application-mixin
                                               lilu-app:application)
  ((cmd-single-p :initarg :cmd-single-p))
  (:default-initargs
   :tags '(:terminal)
   :make-args-fn (lambda(app &key cmd)
                   (with-slots (cmd-single-p) app
                     (%make-terminal-args cmd-single-p cmd)))))

(defmethod run-open-terminal ((app simple-terminal-program-application)
                              &key cmd error-p wait-p)
  (lilu-app:run-simple-program app :cmd cmd :error-p error-p :wait-p wait-p))

(defun register-simple-terminal (string-designator program version-option version-script cmd-single-p)
  (lilu-app:register-application
   (make-instance 'simple-terminal-program-application
                  :name (string string-designator)
                  :program program
                  :version-option version-option
                  :version-script version-script
                  :cmd-single-p cmd-single-p)))

;;;
;;; registered terminals
;;;

(register-simple-terminal :xfce4 "xfce4-terminal" "--version"
                          "xfce4-terminal --version | head -1 | awk '{print $2}'"
                          t)

(register-simple-terminal :xterm "xterm" "-v"
                          "xterm -v | tr '()' ' ' | awk '{print $2}'"
                          t)

(register-simple-terminal :cool-retro-term "cool-retro-term" "--version" ;; no version
                          "cool-retro-term --version 2>&1 | awk '{ print $2 }'"
                          nil)

(register-simple-terminal :terminator "terminator" "--version"
                          "terminator --version | awk '{ print $2 }'"
                          t)

(register-simple-terminal :urxvt "urxvt" "-help"
                          "urxvt -help 2>&1 | head -1 | tr \"v\" \" \" | awk '{ print $5 }'"
                          nil)

(register-simple-terminal :uxterm "uxterm" "-v"
                          "uxterm -v | tr '()' ' ' | awk '{print $2}'"
                          nil)

(register-simple-terminal :alacritty "alacritty" "--version"
                          "alacritty --version | awk '{print $2}'"
                          nil)

(register-simple-terminal :st "st" "-v"
                          "st -v 2>&1 | awk '{print $2}'"
                          nil)

(register-simple-terminal :kitty "kitty" "-v"
                          "kitty -v | awk '{print $2}'"
                          nil)

(register-simple-terminal :terminology "terminology" "--version"
                          "terminology --version | head -1 | awk '{print $2}'"
                          nil)

(register-simple-terminal :tilix "tilix" "--version"
                          "tilix --version | head -2 | tail -1 | awk '{ print $3 }'"
                          nil)

(setf *terminal* (some #'(lambda (app)
                           (when (lilu-app:application-installed-p app)
                             app))
                       (reverse (lilu-app:find-applications :terminal))))
